Ext.define('BenBen.common.BasePanel', {
    extend : 'Ext.panel.Panel',
    requires: [
        'BenBen.common.SearchForm',
        'BenBen.common.FormWindow'
    ],
    layout : 'border',
    border : false,
    /**
     * 默认的功能按钮
     */
    tbarDefaultItems : 'add,update,look,delete',
	initComponent : function() {
        var me = this;
        var config = this.config;
		Ext.define('dataModel', {
			extend : 'Ext.data.Model',
			fields : me.dataModelFields,
			idProperty : 'id'
		});
		this.store = new Ext.create('Ext.data.Store', {
			pageSize : config.pageSize,
			model : 'dataModel',
			remoteSort : true,
			proxy: {
		        type: 'rest',
		        actionMethods: {
	                create : 'POST',
	                read   : 'GET',
	                update : 'POST',
	                destroy: 'POST'
	            },
                url : config.url,
		        reader: {
		            type: 'json',
		            root: 'data',
		            totalProperty: 'totalCount'
		        }
		     }
		});


		this.searchForm = Ext.create('BenBen.common.SearchForm', {
			searchGrid : me.gridPanel,
			gridStore : me.store,
			searchItems : me.searchItem
		});

        var tb  = Ext.create('Ext.toolbar.Toolbar');

        var defalutItems = me.tbarDefaultItems;
        if(defalutItems.indexOf('add') != -1){
            tb.add({
                text : '新增',
                iconCls : 'gridadd',
                handler : Ext.Function.bind(me.addFn, me)
            });
        }

        if(defalutItems.indexOf('update') != -1){
            tb.add({
                text : '修改',
                iconCls : 'gridupdate',
                handler : Ext.Function.bind(me.updateFn, me)
            });
        }

        if(defalutItems.indexOf('look') != -1){
            tb.add({
                text : '查看',
                iconCls : 'gridlook',
                handler : Ext.Function.bind(me.showFn, me)
            });
        }

        if(defalutItems.indexOf('delete') != -1){
            tb.add({
                text : '删除',
                iconCls : 'griddelete',
                handler : Ext.Function.bind(me.deleteFn, me)
            });
        }

        if(me.tbarItems){
            for(var i = 0; i < me.tbarItems.length; i++){
                tb.add(me.tbarItems[i]);
            }

        }



		this.gridPanel = Ext.create('Ext.grid.Panel', {
			selType: 'checkboxmodel',
			collapsible: false,
	        animCollapse: false,
            autoScroll : true,
			columns : me.columns,
			plugins: me.gridPlugins,
			height: document.documentElement.clientHeight/2,
			tbar : tb,
			bbar : Ext.create('Ext.PagingToolbar', {
				store : me.store,
				dock:'bottom',
				displayInfo : '当前记录 {0} - {1} of {2}',
				emptyMsg : '暂无记录'
			}),
			region : 'center',
			margins : '5 5 5 5',
			store : me.store,
			loadMask : true,
			viewConfig : {
				stripeRows : true,
				enableTextSelection : true
			}
		});
        me.store.load({
            params : {
                start : 0,
                limit : config.pageSize
            }
        });
        this.gridPanel.addListener('cellclick',function(grid,td, columnIndex,record) {
            console.log(record.get('_id'));
            this.formId = record.get('_id');
        });

        Ext.apply(this,{
			border : false,
			layout : 'form',
			items : [{
				xtype : 'panel',
				layout : 'fit',
				items : [this.searchForm]
			},{
				xtype : 'panel',
				layout : 'fit',
				items : [this.gridPanel]
			}]
		});

		this.callParent(arguments);
	},
    createEditWindow : function(type,url){
        var me = this;
        var config = this.config;
        if(type == 1){
            me.dataForm.getForm().reset();
        }

        var window = Ext.create('BenBen.common.FormWindow', {
            width : config.winWidth,
            height : config.winHeigth,
            dataForm : me.dataForm,
            config : {
                type : type,
                url : url,
                store : me.getStore()
            }
        });

        return window;
    },
	addFn : function() {
        var url = this.config.url;
		this.createEditWindow(1,url).show();
	},
	updateFn : function() {
        var record = this.getSeletedRecord();
        if(record != null){
            var url = this.config.url;

            url += '/' + record.get('_id');

            var window = this.createEditWindow(2,url);

            this.loadFormData(url);
            window.show();
		}
	},
	showFn : function() {
        var record = this.getSeletedRecord();
        if(record != null){

            var url = this.config.url;

            url += '/' + record.get('_id');

            var window = this.createEditWindow(3 ,url);

            this.loadFormData(url);
            window.show();
        }
	},
	deleteFn : function() {
		if(this.gridPanel.getSelectionModel() != null){
			var record = this.gridPanel.getSelectionModel().getSelection()[0];
			var store = this.gridPanel.store;
			Ext.Ajax.request({
                url: this.deleteUrl,
                params: { id: record.get('id') },
                method: 'POST',
                callback: function (options, success, response) {
                    if (success) {
                        store.reload();
                        Ext.Msg.confirm('成功','删除数据成功');
                       
                    } else {
                        Ext.Msg.confirm('失败','删除数据失败');
                    }
                }
            });
		}
	},

    /**
     * 获取grid的store
     */
    getStore : function(){
        return this.store;
    },

    reload : function(){
        this.getStore().load();
    },

    getSeletedRecord : function(){
        if(this.gridPanel.getSelectionModel() != null){
            return this.gridPanel.getSelectionModel().getSelection()[0];
        }

        return null;
    },

    loadFormData : function(url){
        this.dataForm.getForm().load({
            url : url,
            method:'GET',
            waitMsg : '加载...'
        });
    }
});